#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <X11/XKBlib.h>




int main()
{
    Display*    dpy     = XOpenDisplay(0);
    Window      root    = DefaultRootWindow(dpy);
    XEvent      ev;

    //unsigned int    modifiers       = ControlMask | ShiftMask;
    unsigned int    modifiers       = AnyModifier;
    Window          grab_window     =  root;
    Bool            owner_events    = False;
    int             pointer_mode    = GrabModeAsync;
    int             keyboard_mode   = GrabModeAsync;

    int             keycode;
    keycode = XKeysymToKeycode(dpy, XK_Num_Lock);
    XGrabKey(dpy, keycode, modifiers, grab_window, owner_events, pointer_mode,
             keyboard_mode);
    keycode         = XKeysymToKeycode(dpy, XK_Caps_Lock);
    XGrabKey(dpy, keycode, modifiers, grab_window, owner_events, pointer_mode,
             keyboard_mode);
    //XUngrabKey(dpy,keycode,modifiers,grab_window);
    
    //XSelectInput(dpy, root, KeyPressMask|KeyReleaseMask );
    //XAutoRepeatOn(dpy);
    KeySym keysym;
    char *keystr = NULL;
    while(1)
    {
        XNextEvent(dpy, &ev);
        keysym = XkbKeycodeToKeysym(dpy, ev.xkey.keycode, 
                                    0, ev.xkey.state & ShiftMask ? 1 : 0);
        keystr = XKeysymToString(keysym);
        switch(ev.type) {
        case KeyPress:
            printf("Hot key %s pressed!\n", keystr);
            break;
        case KeyRelease:
            printf("Hot key %s released!\n", keystr);
            break;
        default:
            printf("Unknown event type: %d!\n", ev.type);
            break;
        }

    }

    XCloseDisplay(dpy);
    return 0;
}
